home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15552 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  35 lines

  1. Path: news.mindlink.net!news
  2. From: Allan_Nienhuis@mindlink.bc.ca (Allan Nienhuis)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: returning an array from function
  5. Date: Sat, 06 Apr 1996 14:01:10 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4k5t9b$gvh@fountain.mindlink.net>
  8. References: <4jstd8$kh0@news1.sunbelt.net>
  9. NNTP-Posting-Host: line006.abb.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. bourne@infoave.net (Rick Huebner) wrote:
  13. [snip]
  14. >a pointer to the array, from a function.  I have seen several examples of
  15. >returning a pointer, but it doesn't seem to work for me.  My function is going
  16.  
  17. you are probably returning a pointer to an array which will be out of
  18. scope when the function exits, and is therefore no longer valid.  If
  19. you are creating the array in the function, use the new operator:
  20.  
  21.  
  22. char *String;
  23.  
  24. String = new char[100];
  25.  
  26. you can then return the pointer to String, and it will remain a valid
  27. array, then use delete[]String when you've finished using it outside
  28. of the function to free the memory used by the array. (this is very
  29. important)
  30.  
  31.  
  32.  
  33. Allan_Nienhuis@Mindlink.bc.ca
  34.  
  35.